Page({
  data: {
    isAgree: false
  },

  onAgreeChange(e) {
    this.setData({
      isAgree: e.detail.value.length > 0
    })
  },

  onTapConfirm() {
    if (!this.data.isAgree) {
      wx.showToast({
        title: '请先阅读并同意协议',
        icon: 'none'
      })
      return
    }
    
    // 将同意状态存入缓存
    wx.setStorageSync('hasAgreedToTerms', true)
    
    // 返回注册页面
    wx.navigateBack({
      delta: 1,
      fail: () => {
        // 如果返回失败，则重定向到注册页
        wx.redirectTo({
          url: '/pages/register/register'
        })
      }
    })
  },

  onShareAppMessage() {
    return {
      title: '西科树洞用户服务协议',
      path: '/pages/agreement/agreement'
    }
  },

  onLoad() {
    console.log('Agreement page loaded')
  }
})
